
cmake_minimum_required(VERSION 2.8.12)

project("generated_code")

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)

#Check C++11
include(CheckCXXCompilerFlag)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG OR
        CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    check_cxx_compiler_flag(-std=c++11 SUPPORTS_CXX11)
    if(SUPPORTS_CXX11)
        add_compile_options(-std=c++11)
    else()
        message(FATAL_ERROR "Compiler doesn't support C++11")
    endif()
endif()


# Find requirements
find_package(fastcdr REQUIRED)
find_package(fastrtps REQUIRED)
find_package(foonathan_memory REQUIRED)


set(targetName messData)
set(targetExtensionName .elf)
set(debugSuffix _d)
set(targetFullName ${targetName}${targetExtensionName})
set(debugMacro ETWS_DEBUG)

message("debug = " ${debug})

project(${targetFullName}[C][C++])
message("project name: " ${PROJECT_NAME})
message(STATUS "binary file name: " ${targetFullName})
message("project root dir = " ${PROJECT_SOURCE_DIR})
set(PROJECT_BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/bin)


if(NOT DEFINED crossCompilePath OR crossCompilePath STREQUAL "")
	message(WARNING "host compile.")
	set(cCompilerPath    gcc)
	set(cxxCompilerPath  g++)
else()
	message(WARNING "cross compile.")
	# set(crossCompilePath "/path/to/rootfs")
	set(CMAKE_CROSSCOMPILING TRUE)
	if(NOT DEFINED crossCompileGccPath OR crossCompileGccPath STREQUAL "")
		set(cCompilerPath    aarch64-linux-gnu-gcc)
		set(cxxCompilerPath	aarch64-linux-gnu-g++)
	else()
		set(cCompilerPath    ${crossCompileGccPath}/aarch64-linux-gnu-gcc)
		set(cxxCompilerPath  ${crossCompileGccPath}/aarch64-linux-gnu-g++)
	endif()
endif()
set(CMAKE_C_COMPILER    ${cCompilerPath})
set(CMAKE_CXX_COMPILER  ${cxxCompilerPath})
set(CMAKE_SYSTEM_NAME Linux)

set(CMAKE_SYSROOT ${crossCompilePath})
set(CMAKE_FIND_ROOT_PATH  ${crossCompilePath} ${SYSROOT_PATH})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM  NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY  ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE  ONLY)

set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH	
	"$ORIGIN" 
	"$ORIGIN/lib" 
	"$ORIGIN/../lib" 
	"$ORIGIN/../../lib" 
	
)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)


message(STATUS "cross compile rootfs root path: " ${crossCompilePath})
set(CMAKE_CXX_STANDARD 17)
set(prefix ${CMAKE_CURRENT_SOURCE_DIR})
message(prefix = ${prefix})

##########
set(includePath 
    ${prefix} 
	${prefix}/inc 
	${prefix}/transportLayer 
	${prefix}/mylog/easylogging 
	${prefix}/ddsData/big  
	${prefix}/ddsData/medium   
	${prefix}/ddsData/small   
	${prefix}/ddsData/variable 
)

set(srcPath 
	${prefix} 
    ${prefix}/transportLayer 
	${prefix}/src 
	${prefix}/mylog 
	${prefix}/mylog/easylogging 
	${prefix}/function 
	${prefix}/ddsData/big  
	${prefix}/ddsData/medium   
	${prefix}/ddsData/small   
	${prefix}/ddsData/variable     
)

set(libSrcPath 
	# ${prefix}/src/Proto/FastDDS 

)

set(libs 
	pthread 
	stdc++fs 
	fastcdr 
	fastrtps 
) 


set(projectBuildPath ${prefix}/build)
set(debugFileGeneratePath ${projectBuildPath}/debug)
set(releaseFileGeneratePath ${projectBuildPath}/release)

add_definitions(-DELPP_THREAD_SAFE)
add_definitions(-DELPP_UNICODE)

macro(checkDir dir)
	if(EXISTS ${dir})
		if(IS_DIRECTORY ${dir})
			message("directory \"${dir}\" already exists!")
		else()
			file(RENAME ${dir} ${dir}.old)
			file(MAKE_DIRECTORY ${dir})
			message("rename file ${dir} and create directory: ${dir}")
		endif()
	else()
		file(MAKE_DIRECTORY ${dir})
		message("create new directory: ${dir}")
	endif()
endmacro()

checkDir(${projectBuildPath})

message("project bin dir = " ${PROJECT_BINARY_DIR})
message("current file name: " ${CMAKE_CURRENT_LIST_FILE})
message("current line number: " ${CMAKE_CURRENT_LIST_LINE})

macro(addSuffix srcList suffix)
	foreach(tmp ${${srcList}})
		if(DEFINED tmpList)
			list(APPEND tmpList ${tmp}${suffix})
		else()
			set(tmpList ${tmp}${suffix})
		endif()
	endforeach()
	set(${srcList} ${tmpList})
	unset(tmpList)
	message(${${srcList}})
endmacro()


message("*****************************************************************************************")
if (debug STREQUAL "ON" OR debug STREQUAL "on")
	set(buildVersion debug) 
	# addSuffix(libs _d) 
	# set(librarySuffix _d) 
	checkDir(${debugFileGeneratePath})
	set(CMAKE_CXX_FLAGS "-g -O0 -std=c++${CMAKE_CXX_STANDARD} -v -Wl,--verbose -Wall -fPIC ${CMAKE_CXX_FLAGS}")
else()
	set(buildVersion release)
	checkDir(${releaseFileGeneratePath})
	set(CMAKE_CXX_FLAGS "-std=c++${CMAKE_CXX_STANDARD} -Wall -O3 -fPIC ${CMAKE_CXX_FLAGS}")
endif()
message(WARNING "CXX Flag: " ${CMAKE_CXX_FLAGS})
message("build version: " ${buildVersion})
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/${buildVersion})
message("executable path: " ${EXECUTABLE_OUTPUT_PATH})
message("*****************************************************************************************")

# src sub dir.
foreach(subDir ${srcPath})
	message("child source directory: " ${subDir})
	aux_source_directory(${subDir} srcFiles) 	#top cmake file's variable will be delivered to other sub cmake files,so we destroy it at the end int this CMake file.
endforeach(subDir ${srcPath})

foreach(item ${srcFiles})
	message("file ${item}")
endforeach(item ${srcFiles})

# src header sub dir.
include_directories(${includePath})


# message(STATUS "Configuring messData...")
# add_library(messData_lib messData.cxx)
# target_link_libraries(messData_lib fastcdr fastrtps)

# add_executable(messData messDataPubSubTypes.cxx messDataPublisher.cxx messDataSubscriber.cxx messDataPubSubMain.cxx)
# target_link_libraries(messData  
#         messData_lib )

# lib src sub dir.
foreach(subDir ${libSrcPath})
	message("child library source directory: " ${subDir})
	add_subdirectory(${subDir})
endforeach(subDir ${libSrcPath})


add_executable(${targetFullName} ${srcFiles})
target_link_libraries(${targetFullName} ${libs})

#other internal system information variables
message("CMAKE_MAJOR_VERSION = " ${CMAKE_MAJOR_VERSION})
message("CMAKE_MINOR_VERSION = " ${CMAKE_MINOR_VERSION})
message("CMAKE_PATCH_VERSION = " ${CMAKE_PATCH_VERSION})
message("CMAKE_SYSTEM = " ${CMAKE_SYSTEM})
message("CMAKE_SYSTEM_NAME = " ${CMAKE_SYSTEM_NAME})
message("CMAKE_SYSTEM_VERSION = " ${CMAKE_SYSTEM_VERSION})
message("CMAKE_SYSTEM_PROCESSOR = " ${CMAKE_SYSTEM_PROCESSOR})
message("UNIX = " ${UNIX})

unset(srcFiles)



